package com.life.xxx.aotucode;

import java.sql.Types;
import java.util.Collections;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.baomidou.mybatisplus.generator.model.ClassAnnotationAttributes;

/**
 * @author: csl
 * @description: mysql数据库代码生成
 * @date: 2025-07-22 11:46
 */
public class CodeGenerator {
    public static void main(String[] args) {
        FastAutoGenerator.create("jdbc:mysql://localhost:3306/boring_life?useSSL=false&serverTimezone=UTC", "root", "123456")
                .globalConfig(builder -> {
                    builder.author("csl") // 设置作者
//                          .enableSwagger() // 开启 swagger 模式
                            .disableOpenDir()

                            .outputDir("src/main/java"); // 指定输出目录
                })
                .dataSourceConfig(builder ->
                        builder.typeConvertHandler((globalConfig, typeRegistry, metaInfo) -> {
                            int typeCode = metaInfo.getJdbcType().TYPE_CODE;
                            if (typeCode == Types.SMALLINT) {
                                // 自定义类型转换
                                return DbColumnType.INTEGER;
                            }
                            return typeRegistry.getColumnType(metaInfo);
                        })
                )
                .packageConfig(builder ->
                        builder.parent("com.life.xxx") // 设置父包名
                                .entity("entity")
                                .mapper("mapper")
                                .service("service")
                                .controller("api")
                                .pathInfo(Collections.singletonMap(OutputFile.xml, "src/main/resources/mapper")) // 设置mapperXml生成路径
                )
                .strategyConfig(builder -> {
                    builder.addInclude("life_user") // 设置需要生成的表名
                            .entityBuilder()
                                .disableSerialVersionUID()
                                .enableLombok(new ClassAnnotationAttributes("@Data","lombok.Data")) // 启用 Lombok
                                .enableTableFieldAnnotation() // 启用字段注解
                                .enableFileOverride() //覆盖已生成的文件
                            .controllerBuilder()
                                .formatFileName("%sApi")
                                .enableRestStyle() // 启用 REST 风格
                                .enableFileOverride()
                                .template("/templates/controller.java")
                            .serviceBuilder()
                                .formatServiceFileName("%sService")
                                .formatServiceImplFileName("%sServiceImpl")
                                .serviceImplTemplate("/templates/serviceImpl.java")
                                .serviceTemplate("/templates/service.java")
                                .enableFileOverride()
                            .mapperBuilder()
                                .enableFileOverride()
                                .mapperXmlTemplate("/templates/mapper.xml")
                                .mapperTemplate("/templates/mapper.java");
                })
                .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .execute();
    }
}
